﻿using Coldairarrow.Entity.Backstage.BS_Lables;
using Coldairarrow.Entity.Backstage.BS_Specials;
using Coldairarrow.Entity.BS_Lables;
using Coldairarrow.Entity.DTO;
using Coldairarrow.Entity.Goods;
using Coldairarrow.Entity.PersonalCenter;
using Coldairarrow.IBusiness;
using Coldairarrow.Util;
using EFCore.Sharding;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Goods
{
    public class BTB_GoodsBusiness : BaseBusiness<BTB_Goods>, IBTB_GoodsBusiness, ITransientDependency
    {
        readonly IOperator _operator;
        public BTB_GoodsBusiness(IDbAccessor db, IOperator @operator)
            : base(db)
        {
            _operator = @operator;
        }

        #region 外部接口

        public async Task<BTB_Goods> GetTheDataAsync(string id)
        {
            return await GetEntityAsync(id);
        }


        public async Task<BTB_GoodsDto> GetsGootDataAsync(string id)
        {
            //添加个人足迹
            //if (!_operator.IsNullOrEmpty() && !_operator.UserId.IsNullOrEmpty())
            //{
            //    BTB_browse model = new BTB_browse();
            //    model.id = IdHelper.GetId();
            //    model.CreateTime = DateTime.Now;
            //    model.CreatorId = _operator.UserId;
            //    model.Deleted = false;
            //    model.userid = _operator.UserId;
            //    model.Goodsid = id;

            //    await Db.InsertAsync(model);
            //}

            var goods = await Db.GetIQueryable<BTB_Goods>(true).Where(L => L.ID == id)
                 .Select(
                 a => new BTB_GoodsDto
                 {
                     ID = a.ID,
                     GoodsName = a.GoodsName,
                     GoodsDescribe = a.GoodsDescribe,
                     GoodsPic = a.GoodsPic,
                     Details = a.Details,
                     Stock = a.Stock,
                     SaleCount = a.SaleCount,
                     IsPointGoods = a.IsPointGoods,
                     GoodsPrices = Db.GetIQueryable<BTB_GoodsSpecificationsItem>(true).Where(q => q.GoodsID == a.ID).OrderBy(x => x.Price).FirstOrDefault().Price,
                     PointPrice = Db.GetIQueryable<BTB_GoodsSpecificationsItem>(true).Where(q => q.GoodsID == a.ID).OrderBy(x => x.PointPrice).FirstOrDefault().PointPrice,
                     Collection = Db.GetIQueryable<BTB_Collection>(true).Where(q => q.Goodsid == a.ID),
                     Parametes = Db.GetIQueryable<BTB_GoodsParamete>(true).Where(q => q.Goodsid == a.ID),
                     GoodsPicS = Db.GetIQueryable<BTB_GoodsPic>(true).Where(q => q.GoodsID == a.ID),
                     SpecificationsItem = Db.GetIQueryable<BTB_GoodsSpecificationsItem>(true).Where(q => q.GoodsID == a.ID).OrderBy(x => x.GoodsSpecificationsItemID),
                     LableList = (from c in Db.GetIQueryable<BS_GoodsLable>(true).Where(x => x.GoodId.Equals(a.ID))
                                  join d in Db.GetIQueryable<BS_Lable>(true) on c.LableId equals d.Id
                                  select new GoodLable
                                  {
                                      Id = d.Id,
                                      LableName = d.LableName
                                  }),
                     SpecialList = (from c in Db.GetIQueryable<BS_GoodSpecial>(true).Where(x => x.GoodsId.Equals(a.ID))
                                    join d in Db.GetIQueryable<BS_Special>(true) on c.SpecialId equals d.Id
                                    select new GoodSpecial
                                    {
                                        Id = d.Id,
                                        SpecialName = d.Name
                                    }).OrderBy(x=>x.SpecialName),
                     IsCollected = Db.GetIQueryable<BTB_Collection>(true).Where(x => x.Userid.Equals(_operator.UserId) && x.Goodsid.Equals(a.ID)).Count() > 0 ? true : false
                 }).FirstOrDefaultAsync();

            return goods;
        }

        public async Task<List<BTB_GoodsDto>> GetDataList(string id)
        {
            var goods = Db.GetIQueryable<BTB_Goods>(true).Where(L => L.ID == id)
                 .Select(
                 a => new BTB_GoodsDto
                 {
                     ID = a.ID,
                     GoodsName = a.GoodsName,
                     GoodsDescribe = a.GoodsDescribe,
                     GoodsPic = a.GoodsPic,
                     Details = a.Details,
                     Stock = a.Stock,
                     IsPointGoods = a.IsPointGoods,
                     GoodsPrices = Db.GetIQueryable<BTB_GoodsSpecificationsItem>(true).Where(q => q.GoodsID == a.ID).FirstOrDefault().Price,
                     Collection = Db.GetIQueryable<BTB_Collection>(true).Where(q => q.Goodsid == a.ID),
                     Parametes = Db.GetIQueryable<BTB_GoodsParamete>(true).Where(q => q.Goodsid == a.ID),
                     GoodsPicS = Db.GetIQueryable<BTB_GoodsPic>(true).Where(q => q.GoodsID == a.ID),
                     SpecificationsItem = Db.GetIQueryable<BTB_GoodsSpecificationsItem>(true).Where(q => q.GoodsID == a.ID),
                     LableList = (from c in Db.GetIQueryable<BS_GoodsLable>(true).Where(x => x.GoodId.Equals(a.ID))
                                  join d in Db.GetIQueryable<BS_Lable>(true) on c.LableId equals d.Id
                                  select new GoodLable
                                  {
                                      Id = d.Id,
                                      LableName = d.LableName
                                  }),
                     SpecialList = (from c in Db.GetIQueryable<BS_GoodSpecial>(true).Where(x => x.GoodsId.Equals(a.ID))
                                    join d in Db.GetIQueryable<BS_Special>(true) on c.SpecialId equals d.Id
                                    select new GoodSpecial
                                    {
                                        Id = d.Id,
                                        SpecialName = d.Name
                                    }),
                     IsCollected = Db.GetIQueryable<BTB_Collection>(true).Where(x => x.Userid.Equals(_operator.UserId) && x.Goodsid.Equals(a.ID)).Count()>0?true:false
                 }).ToListAsync();
            return await goods;
        }


        public async Task AddDataAsync(BTB_Goods data)
        {
            BTB_Goods data1 = data;
            data1.ID = GuidHelper.GenerateKey();
            await InsertAsync(data1);
        }

        public async Task UpdateDataAsync(BTB_Goods data)
        {
            await UpdateAsync(data);
        }

        /// <summary>
        /// 商品管理列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<PageResult<BTB_GoodsDto>> GetGoodsList(PageInput<BTB_GoodsDto> input)
        {
            var bTB_GoodsTypeDto = input.Search;

            var result = Db.GetIQueryable<BTB_Goods>();
            //品牌
            if (!string.IsNullOrEmpty(bTB_GoodsTypeDto.GoodsName))
            {
                result = result.Where(k => k.GoodsName.Contains(bTB_GoodsTypeDto.GoodsName));
            }

            //是否特惠
            if (bTB_GoodsTypeDto.Preferential.HasValue)
            {
                result = result.Where(x => x.Preferential.Equals(bTB_GoodsTypeDto.Preferential));
            }

            //是否热销
            if (bTB_GoodsTypeDto.IsHot.HasValue)
            {
                result = result.Where(x => x.IsHot.Equals(bTB_GoodsTypeDto.IsHot));
            }

            //是否今日爆款
            if (bTB_GoodsTypeDto.SpecialType.IsNullOrEmpty())
            {
                result = result.Where(x => x.SpecialType.Equals(bTB_GoodsTypeDto.SpecialType));
            }

            //是否积分商品
            if (bTB_GoodsTypeDto.IsPointGoods.HasValue)
            {
                result = result.Where(x => x.IsPointGoods.Equals(bTB_GoodsTypeDto.IsPointGoods));
            }

            var where = LinqHelper.True<BTB_Goods>();

            int count = result.Count();
            var q = await result.Select(s => new BTB_GoodsDto
            {
                ID = s.ID,
                CreatorId = s.CreatorId,
                GoodsName = s.GoodsName,
                GoodsTypeId = Db.GetIQueryable<BTB_GoodsType>(true).Where(z => z.id == s.RootTypeTwo).FirstOrDefault().TypeName,
                Preferential = s.Preferential,//是否特惠
                MarkingPrice = s.MarkingPrice,
                GoodsDescribe = s.GoodsDescribe,
                GoodsPic = s.GoodsPic,
                Specifications = s.Specifications,
                GoodsPrices = s.GoodsPrices,
                IsHot = s.IsHot,
                Details = s.Details,
                warning = s.warning,
                Stock = Db.GetIQueryable<BTB_GoodsSpecificationsItem>(true).Where(p => p.GoodsID == s.ID).Sum(g => g.Inventory),
                _CreateTime = s.CreateTime,
                Editor = s.Editor,
                EditTime = s.EditTime,
                Deleted = s.Deleted,
                IsPointGoods = s.IsPointGoods,
                PointPrice = s.PointPrice,
                IsSale = s.IsSale,
                LableList = (from c in Db.GetIQueryable<BS_GoodsLable>(true).Where(x => x.GoodId.Equals(s.ID))
                             join d in Db.GetIQueryable<BS_Lable>(true) on c.LableId equals d.Id
                             select new GoodLable
                             {
                                 Id = d.Id,
                                 LableName = d.LableName
                             }),
                SpecialList = (from c in Db.GetIQueryable<BS_GoodSpecial>(true).Where(x => x.GoodsId.Equals(s.ID))
                               join d in Db.GetIQueryable<BS_Special>(true) on c.SpecialId equals d.Id
                               select new GoodSpecial
                               {
                                   Id = d.Id,
                                   SpecialName = d.Name
                               }),
                GoodsPicS = Db.GetIQueryable<BTB_GoodsPic>(true).Where(p => p.GoodsID == s.ID),
                Parametes = Db.GetIQueryable<BTB_GoodsParamete>(true).Where(p => p.Goodsid == s.ID)
            }).Skip((input.PageIndex - 1) * input.PageRows)
                .Take(input.PageRows)
                .ToListAsync();

            return new PageResult<BTB_GoodsDto> { Data = q, Total = count };
        }

        public async Task AddThisData(BTB_GoodsDto data)
        {
            var id = GuidHelper.GenerateKey();

            //参数
            List<BTB_GoodsParamete> gpList = new List<BTB_GoodsParamete>();
            if (data.Parametes.Count() > 0)
            {
                try
                {
                    foreach (var item in data.Parametes)
                    {

                        BTB_GoodsParamete bTB_GoodsParamete1 = new BTB_GoodsParamete()
                        {
                            ParameteId = GuidHelper.GenerateKey(),
                            ParametValue = item.ParametValue,
                            ParameteName = item.ParameteName,
                            CreateTime = DateTime.Now,
                            Editor = _operator.UserId,
                            EditTime = DateTime.Now,
                            CreatorId = _operator.UserId,
                            Deleted = false,
                            Goodsid = id
                        };
                        gpList.Add(bTB_GoodsParamete1);
                    }
                }
                catch (Exception e)
                {
                    throw e;
                }

            }

            //轮播图，多图
            List<BTB_GoodsPic> gpcList = new List<BTB_GoodsPic>();
            if (data.GoodsPicS.Count() > 0)
            {
                foreach (var item in data.GoodsPicS)
                {
                    BTB_GoodsPic bTB_GoodsPic = new BTB_GoodsPic()
                    {

                        Id = GuidHelper.GenerateKey(),
                        CreatorId = _operator.UserId,
                        CreateTime = DateTime.Now,
                        Editor = _operator.UserId,
                        EditTime = DateTime.Now,
                        Pic = item.Pic,
                        Deleted = false,
                        GoodsID = id
                    };

                    gpcList.Add(bTB_GoodsPic);
                }
            }

            //规格
            List<BTB_GoodsSpecificationsItem> gsiList = new List<BTB_GoodsSpecificationsItem>();
            if (data.SpecificationsItem.Count() > 0)
            {
                foreach (var item in data.SpecificationsItem)
                {
                    if (!item.specifications.IsNullOrEmpty())
                    {
                        BTB_GoodsSpecificationsItem SpecificationsItems = new BTB_GoodsSpecificationsItem()
                        {

                            GoodsSpecificationsItemID = GuidHelper.GenerateKey(),
                            CreatorId = _operator.UserId,
                            CreateTime = DateTime.Now,
                            Editor = _operator.UserId,
                            GoodsSpecifications = string.Join(",", item.specifications),
                            EditTime = DateTime.Now,
                            Price = item.Price,
                            PointPrice = item.PointPrice,
                            MarkingPrice = item.MarkingPrice,
                            Inventory = item.Inventory,
                            IsSale = item.IsSale,
                            stock = item.stock,
                            GoodsID = id,
                            Pic = item.Pic,
                            Sale_Count = item.Sale_Count,
                            Deleted = false
                        };
                        gsiList.Add(SpecificationsItems);

                    }
                    else
                    {
                        BTB_GoodsSpecificationsItem SpecificationsItems = new BTB_GoodsSpecificationsItem()
                        {

                            GoodsSpecificationsItemID = GuidHelper.GenerateKey(),
                            CreatorId = _operator.UserId,
                            CreateTime = DateTime.Now,
                            Editor = _operator.UserId,
                            GoodsSpecifications = "",
                            EditTime = DateTime.Now,
                            Price = item.Price,
                            MarkingPrice = item.MarkingPrice,
                            Inventory = item.Inventory,
                            IsSale = item.IsSale,
                            stock = item.stock,
                            GoodsID = id,
                            Pic = item.Pic,
                            Sale_Count = item.Sale_Count,
                            Deleted = false
                        };
                        gsiList.Add(SpecificationsItems);
                    }
                }
            }

            //规格合并/价格
            List<BTB_GoodsSpecifications> gsfList = new List<BTB_GoodsSpecifications>();
            if (data.Specificationsc.Count() > 0)
            {
                foreach (var item in data.Specificationsc)
                {
                    BTB_GoodsSpecifications SpecificationsItems = new BTB_GoodsSpecifications()
                    {
                        GoodsSpecificationsID = GuidHelper.GenerateKey(),
                        CreatorId = _operator.UserId,
                        CreateTime = DateTime.Now,
                        Editor = _operator.UserId,
                        EditTime = DateTime.Now,
                        specification = item.specification,
                        value = item.value,
                        PicUrl = item.PicUrl,
                        Goodsid = id,
                        Deleted = false
                    };
                    gsfList.Add(SpecificationsItems);
                }
            }

            //标签
            List<BS_GoodsLable> gslList = new List<BS_GoodsLable>();
            if (data.LableIds.Length > 0)
            {
                foreach (var item in data.LableIds)
                {
                    BS_GoodsLable bModel = new BS_GoodsLable();
                    bModel.Id = IdHelper.GetId();
                    bModel.CreateTime = DateTime.Now;
                    bModel.CreatorId = _operator.UserId;
                    bModel.Deleted = false;
                    bModel.GoodId = id;
                    bModel.LableId = item;
                    gslList.Add(bModel);
                }
            }

            //List<BS_GoodsLable> gslList = new List<BS_GoodsLable>();
            //if (data.LableList.Count() > 0)
            //{
            //    foreach (var item in data.LableList)
            //    {
            //        BS_GoodsLable bModel = new BS_GoodsLable();
            //        bModel.Id = IdHelper.GetId();
            //        bModel.CreateTime = DateTime.Now;
            //        bModel.CreatorId = _operator.UserId;
            //        bModel.Deleted = false;
            //        bModel.GoodId = id;
            //        bModel.LableId = item.Id;
            //        gslList.Add(bModel);
            //    }
            //}



            //专题
            List<BS_GoodSpecial> splList = new List<BS_GoodSpecial>();
            if (!data.SpecialId.IsNullOrEmpty())
            {
                BS_GoodSpecial bModel = new BS_GoodSpecial();
                bModel.Id = IdHelper.GetId();
                bModel.CreateTime = DateTime.Now;
                bModel.CreatorId = _operator.UserId;
                bModel.Deleted = false;
                bModel.GoodsId = id;
                bModel.SpecialId = data.SpecialId;
                splList.Add(bModel);
            }

            //if (data.SpecialList.Count() > 0)
            //{
            //    foreach (var item in data.SpecialList)
            //    {
            //        BS_GoodSpecial bModel = new BS_GoodSpecial();
            //        bModel.Id = IdHelper.GetId();
            //        bModel.CreateTime = DateTime.Now;
            //        bModel.CreatorId = _operator.UserId;
            //        bModel.Deleted = false;
            //        bModel.GoodsId = id;
            //        bModel.SpecialId = item.Id;
            //        splList.Add(bModel);
            //    }
            //}


            //积分价格
            int data21 = 0;
            decimal data2 = 0.01M;
            decimal? data22 = 0.01M;
            if (data.IsPointGoods.HasValue && data.IsPointGoods == true)
            {
                var dataPointPrice = data.SpecificationsItem.Where(b => b.IsSale == true).OrderBy(x => x.PointPrice).ToList();
                var dataPointPrice1 = data.SpecificationsItem.OrderBy(x => x.PointPrice).ToList();
                if (dataPointPrice.Count() > 0)
                {
                    data21 = dataPointPrice.FirstOrDefault().PointPrice;
                }
                else if (dataPointPrice1.Count() > 0)
                {
                    data21 = dataPointPrice1.FirstOrDefault().PointPrice;

                }
            }
            else
            {
                var dataPrice = data.SpecificationsItem.Where(b => b.IsSale == true).OrderBy(x => x.Price).ToList();
                var dataPrice1 = data.SpecificationsItem.OrderBy(x => x.Price).ToList();

                if (dataPrice.Count() > 0)
                {
                    data2 = dataPrice.FirstOrDefault().Price;
                    data22 = dataPrice.FirstOrDefault().MarkingPrice;
                }
                else if (dataPrice1.Count() > 0)
                {
                    data2 = dataPrice1.FirstOrDefault().Price;
                    data22 = dataPrice.FirstOrDefault().MarkingPrice;
                }
            }

            BTB_Goods data1 = new BTB_Goods()
            {
                ID = id,
                CreatorId = _operator.UserId,
                GoodsName = data.GoodsName,
                GoodsTypeId = data.GoodsTypeId,
                Supplierid = data.Supplierid,
                warning = 0,
                GoodsDescribe = data.GoodsDescribe,
                Preferential = data.Preferential,
                GoodsPrices = data2,
                IsHot = data.IsHot,
                Details = data.Details,
                GoodsPic = data.GoodsPic,
                Parameterid = data.Parameterid,
                Stock = 0,
                CreateTime = data.CreateTime,
                Editor = _operator.UserId,
                EditTime = data.EditTime,
                Deleted = false,
                IsSale = data.IsSale,
                Specifications = data.Specifications,
                SpecialType = data.SpecialType,
                IsPointGoods = data.IsPointGoods,
                PointPrice = data21,
                RootType = data.RootType,
                RootTypeTwo = data.RootTypeTwo,
                MarkingPrice = data22
            };
            await Db.InsertAsync<BTB_Goods>(data1);

            if (gpList.Count > 0)
            {
                await Db.InsertAsync(gpList);
            }

            if (gpcList.Count > 0)
            {
                await Db.InsertAsync(gpcList);
            }

            if (gsiList.Count > 0)
            {
                await Db.InsertAsync(gsiList);
            }

            if (gsfList.Count > 0)
            {
                await Db.InsertAsync(gsfList);
            }

            if (gslList.Count > 0)
            {
                await Db.InsertAsync(gslList);
            }

            if (splList.Count > 0)
            {
                await Db.InsertAsync(splList);
            }
        }

        public async Task UpdateData(BTB_GoodsDto data)
        {
            var pdatas = await Db.GetIQueryable<BTB_GoodsParamete>(false).Where(q => q.Goodsid == data.ID).Select(x => x.ParameteId).ToListAsync();
            if (pdatas.Count > 0)
            {
                await Db.DeleteAsync<BTB_GoodsParamete>(pdatas);

            }

            if (data.Parametes.Count() > 0)
            {
                List<BTB_GoodsParamete> gpList = new List<BTB_GoodsParamete>();
                foreach (var item in data.Parametes)
                {
                    BTB_GoodsParamete bTB_GoodsParamete1 = new BTB_GoodsParamete()
                    {
                        ParameteName = item.ParameteName,
                        ParametValue = item.ParametValue,
                        ParameteId = IdHelper.GetId(),
                        CreateTime = item.CreateTime,
                        Editor = _operator.UserId,
                        EditTime = item.EditTime,
                        CreatorId = item.CreatorId,
                        Deleted = false,
                        Goodsid = data.ID
                    };
                    gpList.Add(bTB_GoodsParamete1);
                }

                await Db.InsertAsync(gpList);
            }

            var datas = await Db.GetIQueryable<BTB_GoodsPic>(false).Where(q => q.GoodsID == data.ID).ToListAsync();
            if (datas.Count() > 0)
            {
                foreach (var item in datas)
                {
                    await Db.DeleteAsync<BTB_GoodsPic>(item);
                }
            }

            List<BTB_GoodsPic> gpcList = new List<BTB_GoodsPic>();
            if (data.GoodsPicS.Count() > 0)
            {
                foreach (var item in data.GoodsPicS)
                {
                    BTB_GoodsPic bTB_GoodsPic = new BTB_GoodsPic()
                    {
                        Id = IdHelper.GetId(),
                        CreatorId = item.CreatorId,
                        CreateTime = data.CreateTime,
                        Editor = _operator.UserId,
                        EditTime = data.EditTime,
                        Pic = item.Pic,
                        Deleted = false,
                        GoodsID = data.ID
                    };
                    gpcList.Add(bTB_GoodsPic);
                }

                await Db.InsertAsync(gpcList);
            }

            var spidatas = await Db.GetIQueryable<BTB_GoodsSpecificationsItem>(false).Where(q => q.GoodsID == data.ID).Select(x => x.GoodsSpecificationsItemID).ToListAsync();
            if (spidatas.Count > 0)
            {
                await Db.DeleteAsync<BTB_GoodsSpecificationsItem>(spidatas);
            }

            if (data.SpecificationsItem.Count() > 0)
            {
                List<BTB_GoodsSpecificationsItem> gsiList = new List<BTB_GoodsSpecificationsItem>();
                foreach (var item in data.SpecificationsItem)
                {
                    if (!item.specifications.IsNullOrEmpty())
                    {
                        BTB_GoodsSpecificationsItem SpecificationsItems = new BTB_GoodsSpecificationsItem()
                        {

                            GoodsSpecificationsItemID = GuidHelper.GenerateKey(),
                            CreatorId = item.CreatorId,
                            CreateTime = data.CreateTime,
                            Editor = _operator.UserId,
                            GoodsSpecifications = string.Join(",", item.specifications),
                            EditTime = data.EditTime,
                            Price = item.Price,
                            PointPrice = item.PointPrice,
                            MarkingPrice = item.MarkingPrice,
                            Inventory = item.Inventory,
                            IsSale = item.IsSale,
                            stock = item.stock,
                            GoodsID = data.ID,
                            Pic = item.Pic,
                            Sale_Count = item.Sale_Count,
                            Deleted = false
                        };
                        gsiList.Add(SpecificationsItems);

                    }
                    else
                    {
                        BTB_GoodsSpecificationsItem SpecificationsItems = new BTB_GoodsSpecificationsItem()
                        {

                            GoodsSpecificationsItemID = GuidHelper.GenerateKey(),
                            CreatorId = item.CreatorId,
                            CreateTime = data.CreateTime,
                            Editor = _operator.UserId,
                            GoodsSpecifications = "",
                            EditTime = data.EditTime,
                            Price = item.Price,
                            MarkingPrice = item.MarkingPrice,
                            Inventory = item.Inventory,
                            IsSale = item.IsSale,
                            stock = item.stock,
                            GoodsID = data.ID,
                            Pic = item.Pic,
                            Sale_Count = item.Sale_Count,
                            Deleted = false
                        };
                        gsiList.Add(SpecificationsItems);
                    }
                }

                await Db.InsertAsync(gsiList);
            }


            var spdatas = await Db.GetIQueryable<BTB_GoodsSpecifications>(false).Where(q => q.Goodsid == data.ID).Select(x => x.GoodsSpecificationsID).ToListAsync();
            if (spdatas.Count > 0)
            {
                await Db.DeleteAsync<BTB_GoodsSpecifications>(spdatas);
            }

            if (data.Specificationsc.Count() > 0)
            {
                List<BTB_GoodsSpecifications> gsfList = new List<BTB_GoodsSpecifications>();
                foreach (var item in data.Specificationsc)
                {
                    BTB_GoodsSpecifications SpecificationsItems = new BTB_GoodsSpecifications()
                    {

                        GoodsSpecificationsID = GuidHelper.GenerateKey(),
                        CreatorId = item.CreatorId,
                        CreateTime = data.CreateTime,
                        Editor = _operator.UserId,
                        EditTime = data.EditTime,
                        specification = item.specification,
                        value = item.value,
                        PicUrl = item.PicUrl,
                        Goodsid = data.ID,
                        Deleted = false
                    };
                    gsfList.Add(SpecificationsItems);
                }

                await Db.InsertAsync(gsfList);
            }

            List<BS_GoodsLable> gslList = new List<BS_GoodsLable>();

            var sIds1 = Db.GetIQueryable<BS_GoodsLable>().Where(x => x.GoodId.Equals(data.ID)).Select(x => x.Id).ToList();
            if (sIds1.Count > 0)
            {
                await Db.DeleteAsync<BS_GoodsLable>(sIds1);
            }
            //标签
            if (data.LableIds.Length > 0)
            {
                foreach (var item in data.LableIds)
                {
                    BS_GoodsLable bModel = new BS_GoodsLable();
                    bModel.Id = IdHelper.GetId();
                    bModel.CreateTime = DateTime.Now;
                    bModel.CreatorId = _operator.UserId;
                    bModel.Deleted = false;
                    bModel.GoodId = data.ID;
                    bModel.LableId = item;
                    gslList.Add(bModel);
                }

                //foreach (var item in data.LableList)
                //{
                //    BS_GoodsLable bModel = new BS_GoodsLable();
                //    bModel.Id = IdHelper.GetId();
                //    bModel.CreateTime = DateTime.Now;
                //    bModel.CreatorId = _operator.UserId;
                //    bModel.Deleted = false;
                //    bModel.GoodId = data.ID;
                //    bModel.LableId = item.Id;
                //    gslList.Add(bModel);
                //}

                await Db.InsertAsync(gslList);
            }


            var sIds = Db.GetIQueryable<BS_GoodSpecial>().Where(x => x.GoodsId.Equals(data.ID)).Select(x => x.Id).ToList();
            if (sIds.Count > 0)
            {
                await Db.DeleteAsync<BS_GoodSpecial>(sIds);
            }
            //专题
            if (!data.SpecialId.IsNullOrEmpty())
            {
                List<BS_GoodSpecial> splList = new List<BS_GoodSpecial>();

                BS_GoodSpecial bModel = new BS_GoodSpecial();
                bModel.Id = IdHelper.GetId();
                bModel.CreateTime = DateTime.Now;
                bModel.CreatorId = _operator.UserId;
                bModel.Deleted = false;
                bModel.GoodsId = data.ID;
                bModel.SpecialId = data.SpecialId;
                splList.Add(bModel);

                await Db.InsertAsync(splList);
            }

            //    foreach (var item in data.SpecialList)
            //    {
            //        BS_GoodSpecial bModel = new BS_GoodSpecial();
            //        bModel.Id = IdHelper.GetId();
            //        bModel.CreateTime = DateTime.Now;
            //        bModel.CreatorId = _operator.UserId;
            //        bModel.Deleted = false;
            //        bModel.GoodsId = data.ID;
            //        bModel.SpecialId = item.Id;
            //        splList.Add(bModel);
            //    }

            //    await Db.InsertAsync(splList);
            //}

            //价格
            decimal data2 = 0.01M;
            decimal? data22 = 0.01M;
            //积分价格
            int data21 = 0;
            if (data.IsPointGoods.HasValue && data.IsPointGoods == true)
            {
                var dataPointPrice = data.SpecificationsItem.Where(b => b.IsSale == true).OrderBy(x => x.PointPrice).ToList();
                var dataPointPrice1 = data.SpecificationsItem.OrderBy(x => x.PointPrice).ToList();
                if (dataPointPrice.Count() > 0)
                {
                    data21 = dataPointPrice.FirstOrDefault().PointPrice;
                }
                else if (dataPointPrice1.Count() > 0)
                {
                    data21 = dataPointPrice1.FirstOrDefault().PointPrice;

                }
            }
            else
            {
                var dataPrice = data.SpecificationsItem.Where(b => b.IsSale == true).OrderBy(x => x.Price).ToList();
                var dataPrice1 = data.SpecificationsItem.OrderBy(x => x.Price).ToList();

                if (dataPrice.Count() > 0)
                {
                    data2 = dataPrice.FirstOrDefault().Price;
                    data22 = dataPrice.FirstOrDefault().MarkingPrice;
                }
                else if (dataPrice1.Count() > 0)
                {
                    data2 = dataPrice1.FirstOrDefault().Price;
                    data22 = dataPrice.FirstOrDefault().MarkingPrice;

                }
            }

            BTB_Goods data1 = new BTB_Goods()
            {
                ID = data.ID,
                CreatorId = _operator.UserId,
                GoodsName = data.GoodsName,
                GoodsTypeId = data.GoodsTypeId,
                RootType = data.RootType,
                RootTypeTwo = data.RootTypeTwo,
                Supplierid = data.Supplierid,
                GoodsDescribe = data.GoodsDescribe,
                Preferential = data.Preferential,
                GoodsPrices = data2,
                warning = data.warning,
                GoodsPic = data.GoodsPic,
                IsHot = data.IsHot,
                Details = data.Details,
                Specifications = data.Specifications,
                Parameterid = data.Parameterid,
                Stock = data.Stock,
                CreateTime = data.CreateTime,
                Editor = _operator.UserId,
                EditTime = data.EditTime,
                Deleted = false,
                IsSale = data.IsSale,
                SpecialType = data.SpecialType,
                IsPointGoods = data.IsPointGoods,
                PointPrice = data21,
                MarkingPrice = data22
            };
            await Db.UpdateAsync<BTB_Goods>(data1);
        }

        public async Task DeleteData(List<string> ids)
        {
            await DeleteAsync(ids);
            foreach (var item in ids)
            {
                var q1 = Db.GetIQueryable<BTB_GoodsPic>(true).Where(q => q.GoodsID == item).ToList();
                await Db.DeleteAsync<BTB_GoodsPic>(q1);
            }
            foreach (var item in ids)
            {
                var q1 = Db.GetIQueryable<BTB_GoodsSpecificationsItem>(true).Where(q => q.GoodsID == item).ToList();

                await Db.DeleteAsync<BTB_GoodsSpecificationsItem>(q1);
            }
            foreach (var item in ids)
            {
                var q1 = Db.GetIQueryable<BTB_GoodsSpecifications>(true).Where(q => q.Goodsid == item).ToList();

                await Db.DeleteAsync<BTB_GoodsSpecifications>(q1);
            }
            foreach (var item in ids)
            {
                var q1 = Db.GetIQueryable<BTB_GoodsParamete>(true).Where(q => q.Goodsid == item).ToList();
                await Db.DeleteAsync<BTB_GoodsParamete>(q1);
            }


            var sIds = Db.GetIQueryable<BS_GoodsLable>().Where(x => ids.Contains(x.GoodId)).Select(x => x.Id).ToList();
            if (sIds.Count > 0)
            {
                await Db.DeleteAsync<BS_GoodsLable>(sIds);
            }
        }

        /// <summary>
        /// AutoMapper
        /// 商品参数展示
        /// </summary>
        /// <param name="id">商品id</param>GetDataList
        /// <returns></returns>
        public object GetTheData(string id)
        {
            var goods = Db.GetIQueryable<BTB_Goods>().Where(q => q.ID == id)
                 .Select(
                 a => new BTB_GoodsDto
                 {
                     ID = a.ID,
                     CreatorId = _operator.UserId,
                     GoodsName = a.GoodsName,
                     GoodsTypeId = a.GoodsTypeId,
                     Supplierid = a.Supplierid,
                     warning = 0,
                     GoodsDescribe = a.GoodsDescribe,
                     Preferential = a.Preferential,
                     GoodsPrices = a.GoodsPrices,
                     IsHot = a.IsHot,
                     Details = a.Details,
                     GoodsPic = a.GoodsPic,
                     Parameterid = a.Parameterid,
                     Stock = 0,
                     CreateTime = a.CreateTime,
                     Editor = _operator.UserId,
                     EditTime = a.EditTime,
                     Deleted = false,
                     IsSale = a.IsSale,
                     Specifications = a.Specifications,
                     RootType = a.RootType,
                     RootTypeTwo = a.RootTypeTwo,
                     MarkingPrice = a.MarkingPrice,
                     SpecialType = a.SpecialType,
                     IsPointGoods = a.IsPointGoods,
                     PointPrice = a.PointPrice,
                     Parametes = Db.GetIQueryable<BTB_GoodsParamete>(true).Where(q => q.Goodsid == a.ID),
                     GoodsPicS = Db.GetIQueryable<BTB_GoodsPic>(true).Where(q => q.GoodsID == a.ID),
                     Specificationsc = Db.GetIQueryable<BTB_GoodsSpecifications>(true).Where(q => q.Goodsid == a.ID),
                     SpecificationsItem = Db.GetIQueryable<BTB_GoodsSpecificationsItem>(true).Where(q => q.GoodsID == a.ID),
                     LableList = (from c in Db.GetIQueryable<BS_GoodsLable>(true).Where(x => x.GoodId.Equals(a.ID))
                                  join d in Db.GetIQueryable<BS_Lable>(true) on c.LableId equals d.Id
                                  select new GoodLable
                                  {
                                      Id = d.Id,
                                      LableName = d.LableName
                                  }),
                     LableIds = Db.GetIQueryable<BS_GoodsLable>(true).Where(x => x.GoodId.Equals(a.ID)).Select(x => x.LableId).ToArray(),
                     SpecialList = (from c in Db.GetIQueryable<BS_GoodSpecial>(true).Where(x => x.GoodsId.Equals(a.ID))
                                    join d in Db.GetIQueryable<BS_Special>(true) on c.SpecialId equals d.Id
                                    select new GoodSpecial
                                    {
                                        Id = d.Id,
                                        SpecialName = d.Name
                                    })
                 }).ToList();


            BTB_GoodsDto model = new BTB_GoodsDto();
            if (goods.Count > 0)
            {
                 model = goods[0];
                var spModel = Db.GetIQueryable<BS_GoodSpecial>(true).Where(x => x.GoodsId.Equals(model.ID)).FirstOrDefault();
                if (!spModel.IsNullOrEmpty())
                {
                    model.SpecialId = spModel.SpecialId;
                }
            }
           
            return model;
        }
        #endregion
        #region 私有成员
        #endregion
    }
}